package com._21cn.framework.flip;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;

/**
 * 翻页列表
 * Created by 司徒海富 on 2016/5/24.
 */
public class PageList<E> extends ArrayList<E> implements Serializable {

    private static final long serialVersionUID = -7317702765506896103L;

    private PageTurn pageTurn;

    public PageList() {
    }

    public PageList(int page, int pageSize, int rowsCount) {
        pageTurn = new PageTurn(page, pageSize, rowsCount);
    }

    /**
     * @return  翻页信息
     */
    public PageTurn getPageTurn() {
        return pageTurn;
    }

    /**
     * @param pageTurn 要设置的翻页信息
     */
    public void setPageTurn(PageTurn pageTurn) {
        this.pageTurn = pageTurn;
    }

    public String toString() {
        return "pageList:" + super.toString() + ",pageTurn:" + pageTurn;
    }

    /**
     * ֻ只是输出JSON格式的数组，不输出pageTurn信息
     *
     * @param dateFormat
     * @param excludeNameSet
     * @return
     */
    public String toJsonArrayExclude(String dateFormat, Set<String> excludeNameSet) {
        SerializeConfig mapping = new SerializeConfig();
        mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
        JSONObject jsonObject = (JSONObject) JSON.toJSON(this);
        //TODO
        jsonObject.remove("pageTurn");
        return JSON.toJSONStringWithDateFormat(jsonObject, dateFormat);
    }

    public String toJsonArray(String dateFormat) {
        return toJsonArrayExclude(dateFormat, null);
    }

    public String toJsonArray() {
        return toJsonArrayExclude(null, null);
    }
}
